package HandshakeJoin;

import java.util.ArrayList;
import java.util.Iterator;
import com.google.common.collect.Lists;
import com.google.common.base.Joiner;
import com.google.common.base.Preconditions;

public class TupleBlock implements Iterable<Tuple>
{
    private ArrayList<Tuple> tuples;
    private int capacity;

    public TupleBlock(final int size) {
        this.capacity = size;
        tuples = Lists.newArrayListWithCapacity(capacity);
    }

    public boolean add(final Tuple tuple) {
        Preconditions.checkState(!isFull(), "TupleBlock buffer is full: size="
                + size());

        return tuples.add(tuple);
    }

    public void clear() {
        tuples.clear();
    }

    public int size() {
        return tuples.size();
    }

    public boolean isEmpty() {
        return tuples.isEmpty();
    }

    public boolean isFull() {
        return (tuples.size() >= capacity);
    }

    public Iterator<Tuple> iterator() {
        return tuples.iterator();
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        Joiner joinerLine = Joiner.on("\n").skipNulls();

        sb.append("----------------\n");
        sb.append(joinerLine.join(tuples));
        sb.append("\n----------------\n");

        return sb.toString();
    }
}